﻿using ICSharpCode.TextEditor.Document;
using System.Collections;
using System.Linq;

namespace DevelopTool.Forms.CommonControls.CodeEditor
{
    public class CodeEditorHelper
    {
        /// <summary>
        /// 根据文件扩展名获取内容类型
        /// </summary>
        /// <param name="fileExtension"></param>
        /// <returns></returns>
        public static string GetContentTypeByFileExtension(string fileExtension)
        {
            var highlightingDefinitions = HighlightingManager.Manager.HighlightingDefinitions;
            foreach(var highlightingDefinition in highlightingDefinitions)
            {
                if(highlightingDefinition is DictionaryEntry dictionaryEntry && dictionaryEntry.Value is DictionaryEntry entry)
                {
                    if(entry.Key is SyntaxMode syntaxMode)
                    {
                        if(syntaxMode.Extensions.Any(it => it == fileExtension)) return syntaxMode.Name;
                    }
                }
            }

            return "Text";
        }

        /// <summary>
        /// 根据内容类型获取文件扩展名
        /// </summary>
        /// <param name="contentType"></param>
        /// <returns></returns>
        public static string GetFileExtensionByContentType(string contentType)
        {
            var highlightingDefinitions = HighlightingManager.Manager.HighlightingDefinitions;
            foreach(var highlightingDefinition in highlightingDefinitions)
            {
                if(highlightingDefinition is DictionaryEntry dictionaryEntry && dictionaryEntry.Value is DictionaryEntry entry)
                {
                    if(entry.Key is SyntaxMode syntaxMode && syntaxMode.Name.ToUpper() == contentType.ToUpper())
                    {
                        return syntaxMode.Extensions.FirstOrDefault();
                    }
                }
            }

            return ".txt";
        }
    }
}
